/*
 * @Author: 肖燕 2461435122@qq.com
 * @Date: 2022-10-17 13:51:27
 * @LastEditors: 肖燕 2461435122@qq.com
 * @LastEditTime: 2023-07-26 09:52:03
 * @FilePath: /mris-platform-all/src/assets/scripts/tools/use-q-list.js
 * @Description: 重点跟踪：问题列表
 */

import { reactive, computed } from '@vue/composition-api'
import { formatTimeRange } from './utils'
import { queryDrgExceptionDetailCount } from '@/services/apis/track'
import moment from 'moment'
import { merge } from 'lodash'

// const query = {
//   dateSelect
// }
const defaultQuery = {
  // 默认时间
  dateSelect: {
    type: 'monthrange',
    value: [
      new Date(moment().subtract(1, 'month').startOf('month').format('YYYY-MM-DD')),
      new Date(moment().subtract(1, 'month').endOf('month').format('YYYY-MM-DD'))
    ]
  }
}
/**
 * @description: 重点跟踪问题列表
 * @param {*} props
 * @param {*} context
 * @param {*} query
 * @return {*}
 */
export default function (props, context, query = {}) {
  // 重点跟踪
  const qTrack = reactive({
    // 重点跟踪：问题列表
    qList: [],
    // 重点跟踪：问题列表Map
    qListMap: new Map(),
    // 重点跟踪：tag颜色
    mapClass: ['orange', 'blue', 'red', 'pink', 'cyan', 'purple'],
    // 重点跟踪：tag默认颜色
    mapClassDef: 'blue',
    // 重点跟踪：所选项
    selTag: ''
  })

  /**
   * @description: 设置重点跟踪标签样式
   * @param {*} row
   * @return {*}
   */
  function setTagColor (row) {
    const selTagItem = qTrack.qListMap.get(row.field_name)
    if (selTagItem) {
      // 给不同的重点追踪设置不同颜色 return selTagItem.className
      return qTrack.mapClassDef
    }
    return qTrack.mapClassDef
  }
  // 起止时间
  const timeObj = computed(() => {
    return function (dateSelect = defaultQuery.dateSelect, timeFormat = 'YYYY-MM') {
      return formatTimeRange(dateSelect, timeFormat)
    }
  })
  /**
   * @description: 获取问题列表
   * @return {*}
   */
  async function handleSearchQList (qFilters) {
    const opts = setOptions(qFilters)
    const res = await queryDrgExceptionDetailCount(opts)
    qTrack.qList = res.data.record.map(item => merge(item, { counts: res.data.total_num_statistics || {} })) || []
  }
  function setOptions (qFilters) {
    return {
      'start_time': timeObj.value(qFilters.dateSelect, 'YYYY-MM-DD 00:00:00').beginDate,
      'end_time': timeObj.value(qFilters.dateSelect, 'YYYY-MM-DD 23:59:59').endDate,
      // 医疗科室
      'medGroupDep.value': qFilters.medGroupDept ? qFilters.medGroupDept || '' : '',
      // 1-全部病案 2-问题病案 默认全部病案
      'query_type': qFilters.pageType ? qFilters.pageType : 1
    }
  }

  return {
    qTrack,
    setTagColor,
    handleSearchQList
  }
}
